package br.com.serctam.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import org.hibernate.Query;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.persistence.HibernateSessionProxy;

import br.com.serctam.entity.Candidato;

/**
 * Servlet implementation class Teste
 */


public class Teste extends HttpServlet {
	private static final long serialVersionUID = 1L;
	

       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Teste() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
//		System.out.println("att   et = " + request.getAttribute("et"));
		System.out.println("param et = " + request.getParameter("et"));
		
		byte[] pdf = null;
		try {
			System.out.println("Teste ..............");
			Connection conn = getJNDIConnection();
			
			String sql = " select boletopdf from boletopdf b, candidato c where c.id = ? and c.boletopdf_id = b.id";
			PreparedStatement  st = conn.prepareStatement(sql);
			st.setInt(1, Integer.parseInt(request.getParameter("et")));
			st.execute();
			ResultSet rs = st.getResultSet();
			if(rs.next()){
				pdf = rs.getBytes(1);
			}
			conn.close();
			
			response.setContentType("application/pdf");
			response.getOutputStream().write(pdf);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
	
	
	static Connection getJNDIConnection(){
	    
		
		InitialContext initContext;
		try {
			initContext = new InitialContext();
			DataSource ds = (DataSource)initContext.lookup("java:/serctamDatasource");
			//Context envContext  = (Context)initContext.lookup("java:/comp/env");
			//DataSource ds = (DataSource)envContext.lookup("java:/serctamDatasource");
			Connection conn = ds.getConnection();
			return conn;
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    return null;
	  }


}
